<template>
	<view class="ku-filling">
		<u-subsection
			:list="list"
			mode="button"
			:current="current"
			@change="saveWareHouseData"
		></u-subsection>
		<view>
			<u-form
				labelPosition="left"
				:model="wareHouseDTO"
				ref="uForm"
				labelWidth="70px"
			>
				<u-form-item
					label="单据编号:"
					prop="billNo"
					borderBottom
					ref="item1"
				>
					<u-textarea
						v-model="wareHouseDTO.billNo" 
						autoHeight
						border="none" 
					></u-textarea>
				</u-form-item>
				<u-form-item
					label="送货单位:"
					prop="supplier"
					borderBottom
					ref="item2"
				>
					<u-textarea 
						v-model="wareHouseDTO.supplier" 
						autoHeight
						border="none" 
					></u-textarea>
				</u-form-item>
				<u-form-item
					label="交货日期:"
					prop="date"
					borderBottom
					ref="item3"
				>
					<u-textarea
						v-model="wareHouseDTO.date" 
						autoHeight
						border="none" 
					></u-textarea>
				</u-form-item>
				<u-form-item
					label="需求班组:"
					prop="purchaseDept"
					borderBottom
					ref="item4"
				>
					<u-textarea
						v-model="wareHouseDTO.purchaseDept" 
						autoHeight
						border="none" 
					></u-textarea>
				</u-form-item>
			</u-form>
		</view>
		<view class="ku-margin-20">
			<u-line color="gray" />
		</view>
		<view class="ku-margin-20">
			<u-input
				v-model="name"
				placeholder="搜索物料名称"
				@change="searchName"
			></u-input>
		</view>
		<view 
			class="material-card" 
			v-for="(item,index) in wareHouserEntryDTOS" 
			:key="index" 
			v-if="item.isShow == true">
			<view>
				<u-row>
					<u-col>
						<view class="ku-margin-20-left">
							{{item.indexShow + '.' + item.materialName}}
						</view>
					</u-col>
					<!-- <u-col span="5">
						<view class="ku-userIfo">
							<u-row>
								<u-button @click="changeNum(index)">修改入库数量</u-button>
							</u-row>
						</view>
					</u-col> -->
				</u-row>
			</view>
			<u-line color="gray" />
			<view class="material-details">
				<u-row>
					<u-col span="7">
						<view class="ku-margin-20-flex-top">
							物料编码:{{item.material}}
						</view>
					</u-col>
					<u-col span="5">
						<view class="ku-margin-20-flex-top">
							单位:{{item.unit}}
						</view>
					</u-col>
				</u-row>
				<u-row>
					<u-col>
						<view class="ku-margin-20-flex">
							<view class="in-num-style">本次入库数量:</view>
							<u--input
								v-model="item.realQty"
								border="surround"
								type="digit"
								@input="handleInput"
								@focus="getIndex(index)"
							></u--input>
						</view>
					</u-col>
				</u-row>
			</view>
		</view>
		<view class="ku-section">
			<u-divider text="没有更多了"></u-divider>
		</view>
	</view>
</template>

<script>
import { barcodeScan, saveWareHouse, submitWareHouse } from "@/api/index.js";
import store from "../../store";
import { objForEach } from "../../utils/util";

export default {
	data() {
		return {
			wareHouseDTO: {
				billNo: "",//单据编号
				supplier: "",//送货单位
				date: "",//交货日期
				purchaseDept: "",//需求班组
			},
			name:'',//物料名称
			searchValue:'',//搜索物料名称
			wareHouserEntryDTOS:[ ],
			current: 0,
			list: ['新增', '保存', '提交'],
			wareHouserId:'',
			checkIndex:'',
			realQty:'',
		}
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow(options) {
	  this.getBarcodeScanData();
	},
	watch: {
		name: {
		  handler() {
			this.searchName()
		  },
		  deep: true,
		},
	},
	methods: {
		//保留两位小数
		handleInput(e) {
			let value = e
			value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
			value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
			this.$nextTick(function() {
				this.wareHouserEntryDTOS[this.checkIndex].realQty = value
			})
		},
		//获取index
		getIndex(data){
			this.checkIndex = data
		}, 
		// 修改入库数量
		// changeNum(i) {
		// 	// console.log("查看修改数量------------>", this.wareHouserEntryDTOS[i])
		// 	this.wareHouserEntryDTOS[i].disable = false
		// 	this.$forceUpdate()
		// },					
		// 使用二维码内带过来的数据  获取填报信息
		getBarcodeScanData() {
		  const _that = this;
		  const params = {
		    number: store.state.number,    
		    user: store.state.userName,
		  };
		  barcodeScan(params).then((res) => {
			  if(res.wareHouseDTO){
				  _that.wareHouseDTO = res.wareHouseDTO;
				  _that.wareHouserEntryDTOS = res.wareHouserEntryDTOS
				  _that.wareHouserEntryDTOS.forEach((item,index) => {
				  	// item.disable = true;
				  	item.isShow = true;
				  	item.indexShow = index+1;
				  	item.maxInput = item.realQty
				  });
			  }
			  else if(res.errorCode == 500){
				  uni.showToast({
				  	title: "本订单物料已完全入库，请勿重复收料！",
				  	icon: "none",
				  	duration: 2000,
				  });
			  }
		  });
		},
		//搜索物料名称
		searchName(){
			const _that = this;
			let changeINdex = 0;
			_that.wareHouserEntryDTOS.forEach((item) => {
				if(item.materialName.indexOf(_that.name) !== -1) {
					item.isShow = true
					changeINdex += 1
					item.indexShow = changeINdex
					// console.log("index1------------>", changeINdex);
				}else{
					item.isShow = false
				}
			});
			_that.$forceUpdate()
		},
		//保存数据、提交数据
		saveWareHouseData(index){	
			//保存
			const _this = this
			if(index == 1){
				const params = {
				  wareHouseDTO: _this.wareHouseDTO,    
				  wareHouserEntryDTOS: _this.wareHouserEntryDTOS,
				};
				saveWareHouse(params).then((res) => {
					if (res.message.isSuccess == true) {
						uni.showToast({
							title: "保存成功！",
							icon: "none",
							duration: 2000,
						});
						_this.current = index;
						// console.log("successEntitys>", res.message.successEntitys);
						_this.wareHouserId = res.message.successEntitys[0].id;
					} else {
						// console.log("successEntitys>", res.message.errors[0].message);
						let mes = res.message.errors[0].message
						uni.showToast({
							title: mes,
							// title: "保存失败！",
							icon: "none",
							duration: 2000,
						});			
					}
				})
				
			} else if (index == 2){
				//提交
				if(_this.wareHouserId){
					const params = {
					  id: _this.wareHouserId,			  
					}; 
					submitWareHouse(params).then((res) => {
						if (res.message.isSuccess == true) {
							uni.showToast({
								title: "已提交！",
								icon: "none",
								duration: 2000,
							});
							this.$navTo('pages/warehouse/index', { })
						} else {
							uni.showToast({
								title: "提交失败！",
								icon: "none",
								duration: 2000,
							});
						}
					})
				}else{
					uni.showToast({
						title: "请先保存！",
						icon: "none",
						duration: 2000,
					});
				}
				
			}
		},
	}
}
</script>

<style lang="scss" scoped>
	.ku-margin-20 {
		margin: 20px;
	}
	.material-card {
		border-radius: 4px;
		margin: 20px;
		background: #F1F3F4;
	}
	.ku-margin-20-flex{
		font-size: 14px;
		display: flex;
		margin-bottom: 10px;
		margin-left: 20px;
		margin-right: 20px;
	}
	.ku-margin-20-left{
		margin-left: 20px;
		margin-top: 10px;
		margin-bottom: 10px;
	}
	.ku-margin-20-top{
		margin-top: 10px;
		
	}
	.ku-margin-20-flex-top{
		font-size: 14px;
		display: flex;
		margin:10px 20px;
	}
	.in-num-style{
		line-height: 38px;
		align-items: center;
		margin-right: 5px;
	}
</style>
